package edu.gatech.recycleright;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JApplet;

import java.util.*;

import javax.swing.Timer;


public class RecycleGame extends JApplet implements Runnable, MouseListener,
		MouseMotionListener, ActionListener {
	/**
	 * version 1
	 */
	private static final long serialVersionUID = 1L;
	int oX1 = 150, oX2=150;
	int oY1 = 200,oY2=200;
	Thread thread;
	Font f;
	int score,leveln=1;
	String scoreS = "Score: " + score;
	String level = "Level: " + leveln;
	Image object1,object2;
	int oCount;
	Random rand = new Random();

	
	int mX,toX1,toY1;
	int mY;
	int dx, dy;
	int xMoved, yMoved;
	
	Timer timer;

	@Override
	public void init() {
		setSize(800, 600);
		setBackground(Color.ORANGE);
		addMouseListener(this);
		addMouseMotionListener(this);
		f = new Font("TimesRoman", Font.BOLD, 24);
		object1 =object2 = getImage(getCodeBase(), "apple.png");
	
		
		timer = new Timer(25, this);
	}

	@Override
	public void start() {
		thread = new Thread(this);
	}

	@Override
	public void run() {
		System.out.println("do you do anything");
	}

	@Override
	public void stop() {

	}

	public void destroy() {
		
	}

	public void paint(Graphics g) {

		Image person = getImage(getCodeBase(), "pane_run_person.png");
		Image blueBin = getImage(getCodeBase(), "blue_container.png");
		Image brownBin = getImage(getCodeBase(), "brown_container.png");
		Image greenBin = getImage(getCodeBase(), "green_container.png");
		Image greyBin = getImage(getCodeBase(), "grey_container.png");
		Image yellowBin = getImage(getCodeBase(), "yellow_container.png");
		g.setFont(f);
		g.clearRect(0, 0, 800, 600);
		g.drawString("LET'S RECYCLE!!!", 100, 100);
		g.drawString(scoreS, 20, 20);
		g.drawString(level,20,60);
		g.drawImage(blueBin, 580, 400, this);
		g.drawString("Paper", 700, 450);
		g.drawImage(brownBin, 500, 100, this);
		g.drawString("Organic", 630, 150);
		g.drawImage(greenBin, 480, 250, this);
		g.drawString("Glass", 600, 300);
		g.drawImage(greyBin, 480, 0, this);
		g.drawString("Household", 600, 50);
		g.drawImage(yellowBin, 480, 500, this);
		g.drawString("Plastic", 600, 550);

		g.drawImage(person, 0, 200, this);
		g.drawImage(object1, oX1, oY1, this);
		g.drawImage(object2, oX2, oY2, this);

		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseMoved(MouseEvent e) {
//		mX = e.getX();
//		mY = e.getY();

	}

	@Override
	public void mouseClicked(MouseEvent e) {


		oCount = rand.nextInt(11);
		if (oCount==1){
			object2 = getImage(getCodeBase(),"cardboard.png");
		}else if(oCount == 2){
		object2 = getImage(getCodeBase(), "bitten_apple_icon.png");}
		else if(oCount==3){
			object2 = getImage(getCodeBase(), "apple.png");
		}
		else if(oCount== 4){
			object2 = getImage(getCodeBase(), "coke.png");
		}
		else if(oCount== 5){
			object2 = getImage(getCodeBase(), "dcoke.png");
		}
		else if(oCount== 6){
			object2 = getImage(getCodeBase(), "news.png");
		}
		else if(oCount== 7){
			object2 = getImage(getCodeBase(), "pen.png");
		}
		else if(oCount== 8){
			object2 = getImage(getCodeBase(), "pencil.png");
		}
		else if(oCount ==9){
			object2 = getImage(getCodeBase(), "cotton.png");
		}
		else if(oCount ==10){
			object2 = getImage(getCodeBase(), "glass_bottle.png");
		}

		else{
			object2 = getImage(getCodeBase(), "glass.png");
		}

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		// thread.start();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

		mX = e.getX();
		mY = e.getY();
		dx = mX - oX2;
		dy = mY - oY2;
		
		oX1 = oX2;
		oY1=oY2;

		if(object2 == getImage(getCodeBase(), "bitten_apple_icon.png")||
				object2 == getImage(getCodeBase(), "apple.png")){
			if(mX>550&&mX<610&&mY>100&&mY<190){
				score+=10;
			}
		}//brown done

		else if(object2==getImage(getCodeBase(),"cardboard.png")||
				object2 == getImage(getCodeBase(), "news.png")){

			if(mX>609&&mX<699&&mY>400&&mY<587){
				score+=10;
			}
		}//blue done
		else if(object2==getImage(getCodeBase(),"coke.png")||
				object2==getImage(getCodeBase(),"dcoke.png")){
			if(mX>510&&mX<595&&mY>500&&mY<583){
				score+=10;
			}
		}//yellow
		else if(
				object2==getImage(getCodeBase(),"pencil.png")||
				object2==getImage(getCodeBase(),"cotton.png")||
						object2==getImage(getCodeBase(),"pen.png")){
			if(mX>520&&mX<600&&mY>0&&mY<85){
				score+=10;
			}
		}//white
		else if(object2==getImage(getCodeBase(),"glass.png")||
				object2==getImage(getCodeBase(),"glass_bottle.png")){
			if(mX>523&&mX<593&&mY>250&&mY<340){
				score+=10;
			}
		}//green done
		timer.start();
		scoreS = "Score: " + score;
		leveln = score/100+1;
		level = "Level: "+leveln;
		object1 = object2;
		repaint();

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		oX1 += dx/10.0;
		oY1 += dy/10.0;
		
		xMoved += Math.abs(dx/10.0);
		yMoved += Math.abs(dy/10.0);
		repaint();
		if (xMoved >=0.9*dx && yMoved >= 0.9*dy){
			timer.stop();
			xMoved = 0;
			yMoved = 0;
		}

		
		
	}


}
